package com.tjetc.service.Impl;

import com.tjetc.dao.UserAddressMapper;
import com.tjetc.entity.UserAddress;
import com.tjetc.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserAddressServiceImpl implements UserAddressService {
    @Autowired
    private UserAddressMapper userAddressMapper;


    /***
     * id查询UserAddress
     * @param addId
     * @return UserAddress
     */
    @Override
    public UserAddress getUserAddressById(Integer addId) {
        return userAddressMapper.selectById(addId);
    }

    /***
     * 查询用户所有收货地址
     * @param userId
     * @return List<UserAddress>
     */
    @Override
    public List<UserAddress> getAllUserAddressByUserId(Integer userId) {
        return userAddressMapper.selectList(userId);
    }

    /***
     * 添加userAddress对象
     * @param userAddress
     * @return 成功返回id，否则返回0
     */
    @Override
    public int add(UserAddress userAddress) {
        userAddress.setCreateTime(LocalDateTime.now());
        userAddress.setUpdateTime(LocalDateTime.now());
        userAddressMapper.add(userAddress);
        change(userAddress.getUserId(),userAddress.getId());
        if(userAddress.getId()>0){
            return userAddress.getId();
        }
        else return 0;
    }

    /***
     * 地址id删除地址
     * @param addId
     * @return
     */
    @Override
    public int deleteUserAddress(Integer addId) {
        return userAddressMapper.deleteById(addId);
    }

    /***
     * 更新地址
     * @param userAddress
     * @return
     */
    @Override
    public int updateUserAddress(UserAddress userAddress) {
        return userAddressMapper.update(userAddress);
    }

    /***
     * 由用户id修改默认地址，1-->0
     * @param userId 用户id addId 地址id
     * @return
     */
    @Override
    public int change(int userId,int addId) {
        return userAddressMapper.change(userId,addId);
    }
}
